package com.example.easeimpro.controller.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.Model;
import com.example.easeimpro.model.bean.UserInfo;
import com.hyphenate.EMValueCallBack;
import com.hyphenate.chat.EMClient;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import java.util.ArrayList;
import java.util.List;
/**
 * 联系人详细页
 */

public class ShowFriendActivity extends BaseActivity {

    private EaseTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_friend);

        titleBar = findViewById(R.id.friend_titlebar);

        InitListener();

    }

    private void InitListener(){
        int pos = getIntent().getIntExtra("itemId",0);//获取当前联系人的下标
        findViewById(R.id.delete_friend_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        //删除联系人
                        deleteFriend(pos);
                        finish();
                    }
                });

            }
        });
        //发送信息
        findViewById(R.id.sendMsg_friend_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String conversationId = getIntent().getStringExtra("conversationId");
                Intent intent = new Intent(mContext,ChatSendMsgActivity.class);
                intent.putExtra("conversationId",conversationId);
                startActivity(intent);
                finish();
            }
        });
        titleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                finish();
            }
        });

    }

    private static final String TAG = "ShowFriendActivity";
    private void deleteFriend(int position){//删除联系人方法
        List<String> hxIds = null;
        try {

            hxIds = EMClient.getInstance().contactManager().getAllContactsFromServer();

            EMClient.getInstance().contactManager().deleteContact(hxIds.get(position-1));
          showToast(EMClient.getInstance().getAccessToken());
            showToast("联系人已删除"+position);
         //   refreshContact();
        } catch (HyphenateException e) {
            showToast(""+e.getMessage());
            e.printStackTrace();
        }
    }
//    private void refreshContact() {//刷新方法
//        //获取数据
//
//        Model.postHandler().post(new Runnable() {
//            @Override
//            public void run() {
//                List<UserInfo> contacts = Model.getInstance().getDbManager().getContactTableDao().getContacts();
//
//                //校验
//                List<EaseUser> easeUsers = new ArrayList<>();
//
//                if(contacts != null && contacts.size()>0){
//
//                    for(UserInfo info : contacts){
//                        EaseUser user = new EaseUser();
//                        user.setUsername(info.getName());
//                        user.setAvatar(info.getName());
//                        user.setNickname(info.getName());
//                        easeUsers.add(user);
//                    }
//                }
//
//            }
//        });
//
//
//
//    }

}
